package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cqvie.model.LeaveMessage;

@Repository("leaveMessageDao")
public class LeaveMessageDao {
	@Resource
	private SessionFactory sessionFactory;

	// 添加请假信息
	@Transactional(propagation = Propagation.REQUIRED)
	public void addMsg(LeaveMessage lmsg) {
		Session session = sessionFactory.getCurrentSession();
		session.save(lmsg);
	}

	// 查询所有的请假信息(返回值中不包括图片)
	@SuppressWarnings("unchecked")
	public List<LeaveMessage> selectLeaveMessage() {
		String hql = "select stuID,stuClass,reason,startDate,endDate from LeaveMessage";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<LeaveMessage> list = query.list();
		if (query.list().size() >= 1) {
			return list;
		} else
			return null;
	}

	// 根据请假人来查询请假信息
	@SuppressWarnings("unchecked")
	public List<LeaveMessage> selectByIDLeaveMsg(int stuID) {
		String hql = "from LeaveMessage lm where lm.student.studentID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, stuID);
		List<LeaveMessage> list = query.list();
		if (query.list().size() >= 1) {
			return list;
		} else
			return null;
	}
}
